/*
* ============================================================================
*  Name        : camengtestappview.cpp
*  Part of     : camengtest
*  Description : 
*  Version     : %version: 2 %
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* You are welcome to, but not required to, contribute any modifications that
* improve stability, usability and/or feature set of the library back to
* Symbian/S60 Camera Wrapper project, as long as binary compatibility is
* retained. See https://projects.forum.nokia.com/camerawrapper for more info.
* ============================================================================
*/

// INCLUDE FILES
#include <coemain.h>
#include <fbs.h>
#include <aknutils.h>
#include "CAMENGTESTAppView.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppView* CCAMENGTESTAppView::NewL( const TRect& aRect )
    {
    CCAMENGTESTAppView* self = CCAMENGTESTAppView::NewLC( aRect );
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppView* CCAMENGTESTAppView::NewLC( const TRect& aRect )
    {
    CCAMENGTESTAppView* self = new ( ELeave ) CCAMENGTESTAppView;
    CleanupStack::PushL( self );
    self->ConstructL( aRect );
    return self;
    }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppView::ConstructL( const TRect& aRect )
    {
    // Create a window for this application view
    CreateWindowL();

    // set the font
    iFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimarySmallFont);
    
    iFrame = new (ELeave) CFbsBitmap;
  
    iText = _L("<wait...>");
    
    // Set the windows size
    SetRect( aRect );

    // Activate the window, which makes it ready to be drawn
    ActivateL();
    }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::CCAMENGTESTAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppView::CCAMENGTESTAppView()
    {
    // No implementation required
    }


// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::~CCAMENGTESTAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CCAMENGTESTAppView::~CCAMENGTESTAppView()
    {
    // No implementation required
    delete iFrame;
    }


// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppView::Draw( const TRect& aRect ) const
    {
    // note that the whole screen is drawn everytime, so aRect-parameter
    // is ignored

    // Get the standard graphics context
    CWindowGc& gc = SystemGc();
    gc.Clear( aRect );
    
    TRect rect( Rect() );
    
    // draw also text, if user has given it via dialog
    if (iText.Length() > 0)
        {       
        gc.UseFont(iFont);
        gc.SetBrushColor(KRgbGray);
        gc.DrawText(iText, rect, Rect().Height()/5, CGraphicsContext::ECenter );
        gc.DiscardFont();
        }
        
    if(iFrame->Handle())
      {
      gc.BitBlt(iVfRect.iTl, iFrame);      
      }
      
    }
    

void CCAMENGTESTAppView::DrawViewFinderFrame( CFbsBitmap* aFrame )
  {    
  iFrame->Reset();
  
  if(aFrame == NULL)
    {   
    return;
    }
  
  TSize frameSize = aFrame->SizeInPixels();
  // recalculate display rect if needed
  if( frameSize != iVfRect.Size() )
    {
    TInt w = frameSize.iWidth;
    TInt h = frameSize.iHeight;
    TInt x = (Rect().iBr.iX - w) >> 1;
    TInt y = (Rect().iBr.iY - h) >> 1;
    if(x < 0) x = 0;
    if(y < 0) y = 0;
    iVfRect = TRect(frameSize);
    iVfRect.Move(x,y);
    }
    
  // Discard previous frame, duplicate the next one
  iFrame->Duplicate(aFrame->Handle());
  DrawNow();
  }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CCAMENGTESTAppView::SizeChanged()
    {
    }

// -----------------------------------------------------------------------------
// CCAMENGTESTAppView::GetText()
// Called by AppUi to either set the text (via dialog) or zero the contents.
// -----------------------------------------------------------------------------
//
TDes & CCAMENGTESTAppView::GetText()
    {
    return iText;
    }

void CCAMENGTESTAppView::SetText( const TDesC& aMsg )
    {
    iText = aMsg;
    DrawNow();
    }
    
void CCAMENGTESTAppView::SetText( const TDesC& aMsg, TInt aVal )
    {
    iText.Format(aMsg, aVal);
    DrawNow();
    }
    
void CCAMENGTESTAppView::SetText( const TDesC& aMsg, TInt aVal1, TInt aVal2 )
    {
    iText.Format(aMsg, aVal1, aVal2);
    DrawNow();
    }
// End of File

